//
//  XCProgressHUD.m
//  CopyAppleMap
//
//  Created by 大江山岚 on 2020/7/1.
//  Copyright © 2020 大江山岚. All rights reserved.
//

#import "XCProgressHUD.h"
#import <Masonry/Masonry.h>

#import "XCHUDCustomInfoView.h"

#define NULLString(string) ((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]])

@interface XCProgressHUD ()
@property (nonatomic, strong, nonnull) UILabel *title_label;
@property (nonatomic, strong, nullable) NSTimer *timer;
@end

@implementation XCProgressHUD

static XCProgressHUD *_instance = nil;

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init];
        _instance.timeInterval = 3;
        _instance.hidden = YES;
        _instance.backgroundColor = UIColor.clearColor;
        _instance.userInteractionEnabled = NO;
        [[UIApplication sharedApplication].windows.lastObject addSubview:_instance];
        [_instance mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo([UIApplication sharedApplication].windows.lastObject);
        }];
    });
    return _instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone
{
    return [XCProgressHUD shareInstance];
}
- (id)copyWithZone:(struct _NSZone *)zone
{
    return [XCProgressHUD shareInstance];
}
- (void)setCustomView:(UIView *)customView
{
    _customView = customView;
    customView.backgroundColor = UIColor.whiteColor;
    customView.layer.cornerRadius = 10;
    customView.layer.shadowColor = UIColor.blackColor.CGColor;
    customView.layer.shadowOffset = CGSizeMake(0, 0);
    customView.layer.shadowRadius = 10;
    customView.layer.shadowOpacity = 0.4;
    customView.hidden = YES;
    [self addSubview:customView];
    [customView setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
    [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(-128);
        make.width.lessThanOrEqualTo(self.mas_width).offset(-128);
    }];
}
- (CGSize)stringSizeWithText:(NSString *)text forFont:(UIFont *)font
{
    NSDictionary *attrs = @{NSFontAttributeName :font};
    CGSize maxSize = CGSizeMake(MAXFLOAT, font.lineHeight);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    return  size;
}

- (void)createTimer
{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:_instance selector:@selector(dismiss) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)show
{
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.hidden = NO;
    } completion:^(BOOL finished) {
        [self createTimer];
    }];
}
- (void)dismiss
{
    self.customView.hidden = YES;
    [self.timer invalidate];
    self.timer = nil;
}

+ (void)show
{
    [[XCProgressHUD shareInstance] show];
}
+ (void)dismiss
{
    [[XCProgressHUD shareInstance] dismiss];
}
+ (void)showHUDWithText:(NSString *)text
{
    if ([XCProgressHUD shareInstance].customView) {
        if ([[XCProgressHUD shareInstance].customView isKindOfClass:XCHUDCustomInfoView.class]) {
            XCHUDCustomInfoView *view = (XCHUDCustomInfoView *)[XCProgressHUD shareInstance].customView;
            view.info_label.text = text;
        }else{
            XCHUDCustomInfoView *view = [[XCHUDCustomInfoView alloc] init];
            view.info_label.text = text;
            [XCProgressHUD shareInstance].customView = view;
        }
    }else{
        XCHUDCustomInfoView *view = [[XCHUDCustomInfoView alloc] init];
        view.info_label.text = text;
        [XCProgressHUD shareInstance].customView = view;
    }
    [[XCProgressHUD shareInstance] show];
}

@end
